from django.db import models
from apps.stations.models import Station

class InstrumentType(models.Model):

    name = models.CharField(max_length=40)
    alias = models.CharField(max_length=40)
    info = models.TextField(blank=True, null=True)
    STATES = (
         (0, 'Inactive'),
         (1, 'Active'),
    )
    status = models.PositiveSmallIntegerField(default=1, choices=STATES)

    class Meta:
        db_table = "instrument_types"
        verbose_name = "Instrument Type"
        verbose_name_plural = "Instrument Types"
    
    def __unicode__(self):
        return u'%s' % self.name

class Instrument(models.Model):

    station = models.ForeignKey(Station)
    itype = models.ForeignKey(InstrumentType)
    code = models.CharField(max_length=5)
    lisn_code = models.CharField(max_length=5)    
    network = models.CharField(max_length=16)
    server = models.CharField(max_length=16, null=True)
    last_file = models.CharField(max_length=40, null=True)
    last_file_date = models.DateTimeField(null=True)
    installation_date = models.DateField(null=True)
    info = models.TextField(blank=True, null=True)
    STATES = (
         (0, 'Inactive'),
         (1, 'Active'),
         (2, 'Stopped'),
         (3, 'Zero Size'),
         (4, 'Same Size'),
         (9, 'Disabled'),                                    
    )
    status = models.PositiveSmallIntegerField(default=2, choices=STATES)

    class Meta:
        db_table = "instruments"
        verbose_name = "Instrument"
        verbose_name_plural = "Instruments"
        unique_together = ('code', 'itype')
    
    def __unicode__(self):
        return u'%s' % self.code
    
    def get_state(self):
    
        return [tup for tup in self.STATES if self.state==tup[0]][1]
